const totalData = Array.from({ length: 100 }, (_, index) => ({
  sn: index + 1,
  tipsImage: `图标提示信息${index + 1}`,
  id: index + 1,
  title: `标题${index + 1}`,
  "appCode": "HELP",
  "basicinfoId": "1858418589618847745",
  "subject": "fasd",
  "processId": "HELP_HelpPublishProcess",
  "drafter": "005213",
  "requester": "005213",
  "requesterNameCn": "马佳俊",
  "requesterDisplayName": "马佳俊,Martin Ma(005213)",
  "requesterDeptNameCn": "企业流程管理科",
  "requesterDeptPathCode": "FIACEH/IT/SH-IT/EIM/BPM",
  "formType": "HelpPublishProcess",
  "formTypeName": "在线帮助",
  "companyCode": "SH01",
  "companyName": "华虹宏力(SH01)",
  "createTime": "2024-11-18 15:54:54"
}))

const recordList = Array.from({ length: 2 }, (_, index) => ({
  sn: index + 1,
  tipsImage: `图标提示信息${index + 1}`,
  id: index + 1,
  "appCode": "HELP",
  "basicinfoId": "1858418589618847745",
  "subject": "fasd",
  "processId": "HELP_HelpPublishProcess",
  "drafter": "005213",
  "requester": "005213",
  "requesterNameCn": "马佳俊",
  "requesterDisplayName": "马佳俊,Martin Ma(005213)",
  "requesterDeptNameCn": "企业流程管理科",
  "requesterDeptPathCode": "FIACEH/IT/SH-IT/EIM/BPM",
  "formType": "HelpPublishProcess",
  "formTypeName": "在线帮助",
  "companyCode": "SH01",
  "companyName": "华虹宏力(SH01)",
  "createTime": "2024-11-18 15:54:54"
}))
//组织数据
const organization = [
  {
      "id": "D0368",
      "pId": null,
      "name": "美国办事处(America Office)",
      "director": "002792",
      "code": "America Office",
      "type": "org",
      "displayName": "美国办事处(America Office)"
  },
  {
    "id": "D0368-2",
    "pId": 'D0368',
    "name": "美国办事处(America Office)-2",
    "director": "0027922",
    "code": "America Office",
    "type": "org",
    "displayName": "美国办事处(America Office)"
},
  {
      "id": "D0357",
      "pId": null,
      "name": "台湾办事处(Taiwan Office)",
      "director": "073184",
      "code": "Taiwan Office",
      "type": "org",
      "displayName": "台湾办事处(Taiwan Office)"
  },
  {
      "id": "D1309",
      "pId": null,
      "name": "华宏置业(HHZY)",
      "director": "103456",
      "code": "HHZY",
      "type": "org",
      "displayName": "华宏置业(HHZY)"
  },
  {
      "id": "D1311",
      "pId": null,
      "name": "财务管理(FM)",
      "director": "000143",
      "code": "Real Estate/FM",
      "type": "org",
      "displayName": "财务管理(FM)"
  }
]
const groupList = [
  {
    "id": "BPM_CGOODS_GOODSJOBVIEWPERSON",
    "groupName": "Customer & Goods Screen_Goods查看所有申请单人员",
    "companyCode": "SH01",
    "appCode": "CGOODS",
    "isAvailable": "Y",
    "displayName": "Customer & Goods Screen_Goods查看所有申请单人员",
      "type": "group"
  },
  {
      "id": "BPM_CEO",
      "groupName": "全局_CEO人员",
      "companyCode": null,
      "appCode": "PR",
      "isAvailable": "Y",
      "displayName": "全局_CEO人员",
      "type": "group"
  },
  {
      "id": "BPM_PR_JOB_VIEW_Y",
      "groupName": "BPM_PR-查看所有申请单_光罩类",
      "companyCode": null,
      "appCode": "PR",
      "isAvailable": "Y",
      "displayName": "BPM_PR-查看所有申请单_光罩类",
      "type": "group"
  }
]
//成员列表
const memberList = [
  {
      "id": "500118",
      "displayName": "安博源,Boyuan An",
      "jobLevel": "6",
      "extension": null,
      "voipExtension": null,
      "cellPhone": null,
      "pathCode": "WX-IT/MES/App",
      "type": "emp"
  },
  {
      "id": "5001183",
      "displayName": "安博源3,Boyuan An",
      "jobLevel": "6",
      "extension": null,
      "voipExtension": null,
      "cellPhone": null,
      "pathCode": "WX-IT/MES/App",
      "type": "emp"
  },
  {
      "id": "5001182",
      "displayName": "安博源2,Boyuan An",
      "jobLevel": "6",
      "extension": null,
      "voipExtension": null,
      "cellPhone": null,
      "pathCode": "WX-IT/MES/App",
      "type": "emp"
  },
  {
      "id": "103046",
      "displayName": "安朝强,Zhaoqiang An",
      "jobLevel": "5",
      "extension": "180-54503",
      "voipExtension": "180-54503",
      "cellPhone": null,
      "pathCode": "ME&S/SH Site/FAB II/TWE/TWT/EE",
      "type": "emp"
  }
]

function buildTree(data) {
  const map = new Map();
  const result = [];

  // 创建一个映射表，以便快速查找每个元素
  data.forEach(item => {
      map.set(item.id, { ...item, children: [] });
  });

  // 构建树结构
  data.forEach(item => {
      const node = map.get(item.id);
      if (item.pId === null || !map.has(item.pId)) {
          // 如果是根节点，添加到结果数组中
          result.push(node);
      } else {
          // 如果不是根节点，找到其父节点并添加到父节点的 children 数组中
          const parent = map.get(item.pId);
          if (parent) {
              parent.children.push(node);
          }
      }
  });

  return result;
}
const data = [
  {
    "pId": null,
    "appCode": "HELP",
    "siteCode": null,
    "companyCode": null,
    "fabCode": null,
    "menuType": "Module",
    "menuNameCn": "工作台",
    "menuNameEn": "Workspace",
    "componentPath": null,
    "resCode": null,
    "targetType": null,
    "iconClass": "item item-workbench",
    "thirdSvcPath": null,
    "paramScripts": null,
    "isDefault": "N",
    "isAvailable": "Y",
    "sortNo": "1",
    "tipsCn": "工作台",
    "tipsEn": "Workspace",
    "remark": null,
    "createdBy": null,
    "createTime": null,
    "updatedBy": null,
    "updateTime": null,
    "id": "HELP_WORKSPACE"
  },
  {
    "pId": "HELP_WORKSPACE",
    "appCode": "HELP",
    "siteCode": null,
    "companyCode": null,
    "fabCode": null,
    "menuType": "MenuGroup",
    "menuNameCn": "申请管理",
    "menuNameEn": "Request Management",
    "componentPath": "/",
    "resCode": null,
    "targetType": null,
    "iconClass": null,
    "thirdSvcPath": null,
    "paramScripts": null,
    "isDefault": "N",
    "isAvailable": "Y",
    "sortNo": "1",
    "tipsCn": "申请管理",
    "tipsEn": "Request Management",
    "remark": null,
    "createdBy": null,
    "createTime": null,
    "updatedBy": null,
    "updateTime": null,
    "id": "HELP_WORKSPACE_REQUEST"
  },
  {
    "pId": "HELP_WORKSPACE",
    "appCode": "HELP",
    "siteCode": null,
    "companyCode": null,
    "fabCode": null,
    "menuType": "MenuGroup",
    "menuNameCn": "应用工具",
    "menuNameEn": "Application Tools",
    "componentPath": "/",
    "resCode": null,
    "targetType": null,
    "iconClass": null,
    "thirdSvcPath": null,
    "paramScripts": null,
    "isDefault": "N",
    "isAvailable": "Y",
    "sortNo": "2",
    "tipsCn": "应用工具",
    "tipsEn": "Application Tools",
    "remark": null,
    "createdBy": null,
    "createTime": null,
    "updatedBy": null,
    "updateTime": null,
    "id": "HELP_WORKSPACE_APPTOOLS"
  },
  {
    "pId": "HELP_WORKSPACE_APPTOOLS",
    "appCode": "HELP",
    "siteCode": null,
    "companyCode": null,
    "fabCode": null,
    "menuType": "MenuGroup",
    "menuNameCn": "查找中心",
    "menuNameEn": "Search Center",
    "componentPath": "/search",
    "resCode": "HELP_SEARCH_CENTER",
    "targetType": null,
    "iconClass": null,
    "thirdSvcPath": null,
    "paramScripts": null,
    "isDefault": "N",
    "isAvailable": "Y",
    "sortNo": "2",
    "tipsCn": "查找中心",
    "tipsEn": "Search Center",
    "remark": null,
    "createdBy": null,
    "createTime": null,
    "updatedBy": null,
    "updateTime": null,
    "id": "HELP_WORKSPACE_APPTOOLS_SEARCH"
  },
        {
          "pId": "HELP_WORKSPACE_REQUEST",
          "appCode": "HELP",
          "siteCode": null,
          "companyCode": null,
          "fabCode": null,
          "menuType": "MenuGroup",
          "menuNameCn": "审批列表",
          "menuNameEn": "Approval List",
          "componentPath": "/list",
          "resCode": "HELP_TO_DO_LIST",
          "targetType": null,
          "iconClass": null,
          "thirdSvcPath": null,
          "paramScripts": null,
          "isDefault": "N",
          "isAvailable": "Y",
          "sortNo": "1",
          "tipsCn": "审批列表",
          "tipsEn": "Approval List",
          "remark": null,
          "createdBy": null,
          "createTime": null,
          "updatedBy": null,
          "updateTime": null,
          "id": "HELP_WORKSPACE_REQUEST_APPROVALLIST3333"
          },
          {
            "pId": "HELP_WORKSPACE_REQUEST",
            "appCode": "HELP",
            "siteCode": null,
            "companyCode": null,
            "fabCode": null,
            "menuType": "MenuGroup",
            "menuNameCn": "测试组件",
            "menuNameEn": "Approval List",
            "componentPath": "/test",
            "resCode": "HELP_TO_DO_LIST",
            "targetType": null,
            "iconClass": null,
            "thirdSvcPath": null,
            "paramScripts": null,
            "isDefault": "N",
            "isAvailable": "Y",
            "sortNo": "1",
            "tipsCn": "测试组件",
            "tipsEn": "Approval List",
            "remark": null,
            "createdBy": null,
            "createTime": null,
            "updatedBy": null,
            "updateTime": null,
            "id": "ae0092iol"
            },
        {
        "pId": "HELP_WORKSPACE_REQUEST",
        "appCode": "HELP",
        "siteCode": null,
        "companyCode": null,
        "fabCode": null,
        "menuType": "MenuGroup",
        "menuNameCn": "Iframe",
        "menuNameEn": "If",
        "componentPath": "/newFrame2",
        "resCode": "HELP_TO_DO_LIST",
        "targetType": "IFRAME_CONTAINER",
        "iconClass": null,
        "thirdSvcPath": null,
        "paramScripts": null,
        "isDefault": "N",
        "isAvailable": "Y",
        "sortNo": "1",
        "tipsCn": "Iframe",
        "tipsEn": "If",
        "remark": null,
        "createdBy": null,
        "createTime": null,
        "updatedBy": null,
        "updateTime": null,
        "id": "HELP_WORKSPACE_REQUEST_APPROVALLIST2"
        },
        {
          "pId": "HELP_WORKSPACE_REQUEST",
          "appCode": "HELP",
          "siteCode": null,
          "companyCode": null,
          "fabCode": null,
          "menuType": "MenuGroup",
          "menuNameCn": "Iframe新窗口",
          "menuNameEn": "If",
          "componentPath": "/newFrame",
          "resCode": "HELP_TO_DO_LIST",
          "targetType": "NEW_WINDOW",
          "iconClass": null,
          "thirdSvcPath": null,
          "paramScripts": null,
          "isDefault": "N",
          "isAvailable": "Y",
          "sortNo": "1",
          "tipsCn": "Iframe新窗口",
          "tipsEn": "If",
          "remark": null,
          "createdBy": null,
          "createTime": null,
          "updatedBy": null,
          "updateTime": null,
          "id": "HELP_WORKSPACE_REQUEST_APPROVALLIST23222"
          },
        {
          "pId": "HELP_WORKSPACE_REQUEST",
          "appCode": "HELP",
          "siteCode": null,
          "companyCode": null,
          "fabCode": null,
          "menuType": "Menu",
          "menuNameCn": "申请创建",
          "menuNameEn": "Create Application",
          "componentPath": "/addForm?helpCode=200",
          "resCode": "HELP_CREATE",
          "targetType": null,
          "iconClass": null,
          "thirdSvcPath": null,
          "paramScripts": null,
          "isDefault": "N",
          "isAvailable": "Y",
          "sortNo": "2",
          "tipsCn": "申请创建",
          "tipsEn": "Create Application",
          "remark": null,
          "createdBy": null,
          "createTime": null,
          "updatedBy": null,
          "updateTime": null,
          "id": "HELP_WORKSPACE_REQUEST_CREATE"
        },
        {
              "pId": null,
              "appCode": "HELP",
              "siteCode": null,
              "companyCode": null,
              "fabCode": null,
              "menuType": "Module",
              "menuNameCn": "管理工具",
              "menuNameEn": "Tools",
              "componentPath": null,
              "resCode": null,
              "targetType": null,
              "iconClass": "item item-tool",
              "thirdSvcPath": null,
              "paramScripts": null,
              "isDefault": "N",
              "isAvailable": "Y",
              "sortNo": "4",
              "tipsCn": "管理工具",
              "tipsEn": "Tools",
              "remark": null,
              "createdBy": null,
              "createTime": null,
              "updatedBy": null,
              "updateTime": null,
              "id": "HELP_TOOLS"
            },
            {
              "pId": "HELP_TOOLS",
              "appCode": "HELP",
              "siteCode": null,
              "companyCode": null,
              "fabCode": null,
              "menuType": "MenuGroup",
              "menuNameCn": "查找中心",
              "menuNameEn": "Search Center",
              "componentPath": '/search',
              "resCode": null,
              "targetType": null,
              "iconClass": null,
              "thirdSvcPath": null,
              "paramScripts": null,
              "isDefault": "N",
              "isAvailable": "Y",
              "sortNo": "1",
              "tipsCn": "查找中心",
              "tipsEn": "Search Center",
              "remark": null,
              "createdBy": null,
              "createTime": null,
              "updatedBy": null,
              "updateTime": null,
              "id": "HELP_TOOLS_TOOLS"
            },
            {
              "pId": null,
              "appCode": "HELP",
              "siteCode": null,
              "companyCode": null,
              "fabCode": null,
              "menuType": "Module",
              "menuNameCn": "在线帮助",
              "menuNameEn": "Online Help",
              "componentPath": null,
              "resCode": null,
              "targetType": null,
              "iconClass": "item item-help",
              "thirdSvcPath": null,
              "paramScripts": null,
              "isDefault": "N",
              "isAvailable": "Y",
              "sortNo": "5",
              "tipsCn": "在线帮助",
              "tipsEn": "Online Help",
              "remark": null,
              "createdBy": null,
              "createTime": null,
              "updatedBy": null,
              "updateTime": null,
              "id": "HELP_HELP"
            },
            {
              "pId": "HELP_HELP",
              "appCode": "HELP",
              "siteCode": null,
              "companyCode": null,
              "fabCode": null,
              "menuType": "MenuGroup",
              "menuNameCn": "在线帮助",
              "menuNameEn": "Online Help",
              "componentPath": '/search',
              "resCode": "HELP_HELP2",
              "targetType": null,
              "iconClass": null,
              "thirdSvcPath": null,
              "paramScripts": null,
              "isDefault": "N",
              "isAvailable": "Y",
              "sortNo": "1",
              "tipsCn": "在线帮助",
              "tipsEn": "Online Help",
              "remark": null,
              "createdBy": null,
              "createTime": null,
              "updatedBy": null,
              "updateTime": null,
              "id": "help22323"
            }
]
console.log('data', buildTree(data))
// 初始化请求计数器
let requestCounter = 0
module.exports=[
    {url:'/list', type:'post',response:res=>{
        console.log('res', res)
        // 解析请求体
        const { body } = res;
        const { current, pageSize } = JSON.parse(body);
        // 计算分页数据
        const startIndex = (current - 1) * pageSize;
        const endIndex = startIndex + pageSize;
        const paginatedData = totalData.slice(startIndex, endIndex);
        // 构建响应数据
        const resp = {
          code: 20000,
          message: 'Success',
          data: {
            total: totalData.length,
            list: paginatedData
          }
        };

        return resp
  }},
  {url:'/getMenu',type:'post',response:res=>{
      console.log('getMenu', res)
      return {
        code: 20000,
        data: buildTree(data)
      };
  }},
  {url:'/getPermission',type:'post',response:res=>{
      console.log(res)
      return {
        code: 20000,
        data: true
      };
  }},
  {url:'/organizationList', type:'post', response: res=>{
      console.log(res)
      return {
        code: 20000,
        data: {
          list: organization
        }
      };
  }},
  {url:'/groupList', type:'post', response: res=>{
      console.log(res)
      return {
        code: 20000,
        data: {
          list: groupList
        }
      };
  }},
  {url:'/memberList', type:'post', response: res=>{
      console.log(res)
      return {
        code: 20000,
        data: {
          list: memberList
        }
      };
  }},
  //审批记录
  {url:'/recordList', type:'post', response: res=>{
      console.log(res)
      return {
        code: 20000,
        data: {
          list: recordList
        }
      };
  }},
  //表格里的测试连接
  {url:'/getLinkUrl', type:'post', response: res=>{
      console.log(res)
      return {
        code: 20000,
        data: {
          url: 'https://www.baidu.com'
        }
      };
  }},
  //上传
  {url:'/upload', type:'post', response: res=>{
      // 解析请求体
      const { body } = res
      // 增加请求计数器
      requestCounter++
      // 判断是否为第二次请求
      if (requestCounter === 2) {
        // 重置计数器（可选）
        requestCounter = 0
        setTimeout(() => {
          // 返回错误响应
          return {
            data: null
          }
        }, 800)
        
      } else {
        setTimeout(() => {
          // 返回成功响应
          return {
            code: 20000,
            data: body
          }
        }, 800)
      }
  }},
  //异步验证
  {url:'/checkValue', type:'post', response: res=>{
      console.log(res)
      return {
        code: 20000,
        data: {
          value: 'hello'
        }
      }
  }},
  //创建
  {url:'/createData', type:'post', response: res=>{
      console.log(res)
      return {
        code: 20000,
        data: {
          message: '创建成功'
        }
      }
  }}
]